﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace Mpi.Common
{
    public static class ColorMapper
    {
        private const int steps = 255;
        //private decimal min;
        //private decimal max;
        //private Color minColor;
        //private Color maxColor;


        //public decimal Min
        //{
        //    get { return min; }
        //    set
        //    {
        //        if (value > max)
        //            throw new ArgumentException("The maximum must be greater than the minimum");
        //        max = value;
        //        min = value;
        //    }
        //}

        //public decimal Max
        //{
        //    get { return max; }
        //    set
        //    {
        //        if (value <= min)
        //            throw new ArgumentException("The maximum must be greater than the minimum");
        //        max = value;
        //    }
        //}

        //public Color MinColor
        //{
        //    get { return minColor; }
        //    set { minColor = value; }
        //}

        //public Color MaxColor
        //{
        //    get { return maxColor; }
        //    set { maxColor = value; }
        //}

        //public ColorMapper(decimal min, decimal max, Color minColor, Color maxColor)
        //{
        //    if (max <= min)
        //        throw new ArgumentException("The maximum must be greater than the minimum");

        //    this.min = min;
        //    this.max = max;
        //    this.minColor = minColor;
        //    this.maxColor = maxColor;
        //}


        public static Color MapValue(decimal val, decimal min, decimal max, Color minColor, Color maxColor)
        {
            return ColorMapper.MapValue(val, min, max, minColor,maxColor,255);
        }

        public static Color MapValue(decimal val, decimal min, decimal max, Color minColor, Color maxColor, int alphaChannel)
        {
            if (max < min)
                throw new ArgumentException("The Min can't be greate than the Max");

            decimal valx = Math.Max(Math.Min(val, max), min); 
           
            decimal rDiff = maxColor.R - minColor.R;
            decimal gDiff = maxColor.G - minColor.G;
            decimal bDiff = maxColor.B - minColor.B;

            decimal rStep = rDiff / ColorMapper.steps;
            decimal gStep = gDiff / ColorMapper.steps;
            decimal bStep = bDiff / ColorMapper.steps;

            decimal factor = (max - min) / ColorMapper.steps;

            decimal x = (valx - min) / factor;

            int r = (int)(minColor.R + Math.Floor((double)(rStep * x)));
            int g = (int)(minColor.G + Math.Floor((double)(gStep * x)));
            int b = (int)(minColor.B + Math.Floor((double)(bStep * x)));
            Color c = minColor;
            try
            {
                c = Color.FromArgb(alphaChannel, r, g, b);
            }
            catch (ArgumentException ae)
            {
                string s = ae.Message;
            }
            return c;
        }

        public static Color MapValue(decimal value, ColorMapping cm)
        {
            return ColorMapper.MapValue(value, cm.Min, cm.Max, cm.MinColor, cm.MaxColor, cm.AlphaChannel);
        }
    }


    public class ColorMapping
    {
        private decimal _min;

        public decimal Min
        {
            get { return _min; }
            set { _min = value; }
        }

        private decimal _max;

        public decimal Max
        {
            get { return _max; }
            set { _max = value; }
        }

        private Color _minColor;

        public Color MinColor
        {
            get { return _minColor; }
            set { _minColor = value; }
        }


        private Color _maxColor;

        public Color MaxColor
        {
            get { return _maxColor; }
            set { _maxColor = value; }
        }

        private int _alphaChannel = 255;

        public int AlphaChannel
        {
            get { return _alphaChannel = 255; }
            set { _alphaChannel = Math.Min(value,255); }
        }



    }

    //public static class ThreeColorMapper : ColorMapper
    //{
    //    private Color middleColor;
    //    private decimal percentation;


    //    public decimal Percentation
    //    {
    //        get { return percentation; }
    //        set
    //        {
    //            if (value < 0)
    //                this.percentation = 0;
    //            else if (value > 100)
    //                this.percentation = 100;
    //            else
    //                percentation = value;
    //        }
    //    }

    //    public Color MiddleColor
    //    {
    //        get { return middleColor; }
    //        set { middleColor = value; }
    //    }

    //    public ThreeColorMapper(decimal min, decimal max, Color minColor, Color middleColor, Color maxColor, decimal percentation)
    //        : base(min, max, minColor, maxColor)
    //    {
    //        this.middleColor = middleColor;
    //        this.percentation = percentation;
    //    }

    //    //public ThreeColorMapper(decimal min, decimal max, ColorAssignment ca)
    //    //    : base(min, max, ca.MinColor, ca.MaxColor)
    //    //{
    //    //    this.middleColor = ca.MiddleColor;
    //    //    this.percentation = ca.Percentation;
    //    //}


    //    public new Color MapValue(decimal val)
    //    {
    //        return this.MapValue(val, 255);
    //    }

    //    public new Color MapValue(decimal val, int alphaChannel)
    //    {


    //        if (this.Percentation == 0)
    //        {
    //            ColorMapper cm = new ColorMapper(this.Min, this.Max, this.MiddleColor, this.MaxColor);
    //            return cm.MapValue(val, alphaChannel);
    //        }
    //        else if (this.Percentation == 0)
    //        {
    //            ColorMapper cm = new ColorMapper(this.Min, this.Max, this.MinColor, this.MiddleColor);
    //            return cm.MapValue(val, alphaChannel);
    //        }
    //        else
    //        {
    //            decimal middelMinMax = this.Min + ((this.Max - this.Min) * (this.Percentation / 100));
    //            if (val <= middelMinMax)
    //            {
    //                ColorMapper cm = new ColorMapper(this.Min, middelMinMax, this.MinColor, this.MiddleColor);
    //                return cm.MapValue(val, alphaChannel);
    //            }
    //            else
    //            {
    //                ColorMapper cm = new ColorMapper(middelMinMax, this.Max, this.MiddleColor, this.MaxColor);
    //                return cm.MapValue(val, alphaChannel);
    //            }
    //        }


    //    }
   // }
}
